package com.jinxin.platform.cdacscontrolmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacscontrolmgr.bean.CdyardmgrCarCategory;
import com.jinxin.platform.cdacscontrolmgr.common.SysOrgInfo;
import com.jinxin.platform.cdacscontrolmgr.mapper.CdacscontrolmgrCarCategoryMapper;
import com.jinxin.platform.cdacscontrolmgr.rpc.CdacscontrolmgrRpc;
import com.jinxin.platform.cdacscontrolmgr.service.ICdacscontrolmgrCarCategoryService;
import com.jinxin.platform.cdacscontrolmgr.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-06
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class CdacscontrolmgrCarCategoryServiceImpl implements ICdacscontrolmgrCarCategoryService {
    @Autowired
    CdacscontrolmgrCarCategoryMapper cdacscontrolmgrCarCategoryMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdacscontrolmgrRpc cdacscontrolmgrRpc;

    @Override
    public CdyardmgrCarCategory selectOne(HashMap param) {
        return cdacscontrolmgrCarCategoryMapper.selectOne(param);
    }

    @Override
    public List<CdyardmgrCarCategory> list(HashMap param) {
        return cdacscontrolmgrCarCategoryMapper.list(param);
    }

    @Override
    public List<CdyardmgrCarCategory> listPage(HashMap param) {
        return cdacscontrolmgrCarCategoryMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return cdacscontrolmgrCarCategoryMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("Id不存在");
        }
        int result = cdacscontrolmgrCarCategoryMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdyardmgrCarCategory record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            SysOrgInfo currentOrg = cdacscontrolmgrRpc.getCurrentOrg();
            if(currentOrg == null){
                return JsonResult.error("获取组织信息失败");
            }
            record.setId(String.valueOf(idWorker.nextId()));
            record.setCreateTime(LocalDateTime.now());
            record.setId(String.valueOf(idWorker.nextId()));
            record.setOrgId(currentOrg.getOrgId());
            count = cdacscontrolmgrCarCategoryMapper.insert(record);
            msg = "新增成功";
        }else {
            count = cdacscontrolmgrCarCategoryMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public String getTemCarCategoryId(String orgId) {
        HashMap param = new HashMap();
        param.put("name","临停");
        param.put("orgId",orgId);
        CdyardmgrCarCategory carCategory = cdacscontrolmgrCarCategoryMapper.selectOne(param);
        if(carCategory != null){
            return carCategory.getId();
        }else {
            param.put("name","临时");
            CdyardmgrCarCategory carCategory2 = cdacscontrolmgrCarCategoryMapper.selectOne(param);
            return carCategory2 != null ? carCategory2.getId() : "";
        }
    }

}
